package com.luyuan.wxapp.controller;

import com.luyuan.common.core.domain.AjaxResult;
import com.luyuan.common.utils.SecurityUtils;
import com.luyuan.gallery.domain.ExhiLikes;
import com.luyuan.gallery.service.IExhiRedisLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Redis点赞记录Controller
 * 
 * @author luyuan
 * @date 2025-11-19
 */
@Api(value = "微信小程序展览接口", tags = "wx_redis_like")
@RestController
@RequestMapping("/wxapp/redis/likes")
public class WxappExhiRedisLikeController extends WxBaseController {
    
    @Autowired
    private IExhiRedisLikeService exhiRedisLikeService;

    /**
     * 点赞或取消点赞
     */
    @ApiOperation(value = "点赞或取消点赞", notes = "点赞或取消点赞", nickname = "likeOrUnlike")
    @PostMapping("/{bizType}/{bizId}")
    public AjaxResult likeOrUnlike(@PathVariable Integer bizType, @PathVariable Long bizId) {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getWxappLoginUser().getUserId();
        boolean result = exhiRedisLikeService.likeOrUnlike(bizId, bizType, userId);
        
        Map<String, Object> data = new HashMap<>();
        data.put("liked", result);
        data.put("likeCount", exhiRedisLikeService.getLikeCount(bizId, bizType));
        
        return AjaxResult.success("操作成功", data);
    }

    /**
     * 获取点赞信息
     */
    @ApiOperation(value = "获取点赞信息", notes = "获取点赞信息", nickname = "getLikeInfo")
    @GetMapping("/{bizType}/{bizId}")
    public AjaxResult getLikeInfo(@PathVariable Integer bizType, @PathVariable Long bizId) {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getWxappLoginUser().getUserId();
        
        Map<String, Object> data = new HashMap<>();
        data.put("liked", exhiRedisLikeService.isLiked(bizId, bizType, userId));
        data.put("likeCount", exhiRedisLikeService.getLikeCount(bizId, bizType));
        data.put("likeTime", exhiRedisLikeService.getUserLikeTime(bizId, bizType, userId));
        
        return AjaxResult.success("查询成功", data);
    }
}